#!/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: Realme.py
#!/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: Oppo.py
#!/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: Hornor.py
# !/user/bin/python
# _*_ coding: UTF-8 _*_
# Filename: Huawei.py
import time
import pymongo
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

client = pymongo.MongoClient('mongodb://localhost:27017')
mydb = client["jdmpresystem"]
product = mydb['Authorproducts']

driver = webdriver.Chrome()
# 修改浏览器部分属性，绕过检测。
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
    "source": """Object.defineProperty(navigator,'webdriver',{get:()=> false})"""
})
driver.get('https://www.opposhop.cn/')
driver.implicitly_wait(10)  # 设置浏览器的等待（耗时） 智能化等待 加载完就不会等待
driver.maximize_window()

# driver.find_element_by_xpath('//*[@id="search-kw"]').send_keys('手机')
# driver.find_element_by_xpath('//*[@id="search-bar-form"]/form/input[2]').click()
driver.find_element_by_xpath('//*[@id="app"]/div/header/div/div[1]/div[2]/div[2]/input').click()
time.sleep(1)
driver.find_element_by_class_name('v-select__slot').find_element_by_xpath('input').send_keys('手机')
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div/div[1]/div/div/div[1]/div[1]/div/button/span').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div/div[2]/div/div[1]/div[3]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div/div[2]/div/div[2]/div[2]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div/div[1]/div[2]/div/div[2]').click()
time.sleep(2)
def drop_down():
    total = driver.find_element_by_css_selector('#app > div.v-dialog__content.v-dialog__content--active > div > div > div.scroll-warp > div.scroll-warp-list > div > div.five-warp > div:nth-child(10) ')
    driver.execute_script('arguments[0].scrollIntoView(true);', total)
    total = driver.find_element_by_css_selector(
        '#app > div.v-dialog__content.v-dialog__content--active > div > div > div.scroll-warp > div.scroll-warp-list > div > div.five-warp > div:nth-child(20) ')
    driver.execute_script('arguments[0].scrollIntoView(true);', total)
drop_down()
time.sleep(1)

lis = driver.find_elements_by_css_selector('#app > div.v-dialog__content.v-dialog__content--active > div > div > div.scroll-warp > div.scroll-warp-list > div > div.five-warp > div')
for li in lis:
    skuname = li.find_element_by_xpath('a/div[2]').text
    price = li.find_element_by_class_name('g-price-original-integer').text
    price = int(price)
    url = li.find_element_by_xpath('a').get_attribute('href')
    img = li.find_element_by_css_selector(' a > div.v-image.v-responsive.goods-card-img.theme--light > div.v-image__image.v-image__image--contain').get_attribute('style')
    img = img.split('"')[1]
    phone = {
        'brand': "真我（realme）",
        'price': price,
        'comment': -1,
        'url': url,
        'skuname': skuname,
        'img': img,
        'logourl': 'http://localhost:8002/ShopLogo/rm.png'
    }
    # print(phone)
    x = product.insert_one(phone)
    print(x)

